package com.banshuibao.bsb_android.activity;

import android.os.Bundle;
import android.widget.ListView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.adapter.CoachInterpretationAdapter;
import com.banshuibao.bsb_android.adapter.CoachListAdapter;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.CoachModel;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshBase;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshListView;

/**
 * Created by zyh on 2017/12/18.
 */

public class CoachListActivity extends BaseActivity implements PullToRefreshBase.OnRefreshListener2<ListView> {

    private PullToRefreshListView pullToRefreshListView;

    private ListView listView;

    private CoachListAdapter adapter;

    private int sinceId = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coach_list);

        initView();
    }

    private void initView() {
        setTitleText("政策解读");
        pullToRefreshListView = findViewById(R.id.pull);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        pullToRefreshListView.loadBar();
        pullToRefreshListView.setOnRefreshListener(this);
        listView = pullToRefreshListView.getRefreshableView();
        listView.setDivider(null);
        adapter = new CoachListAdapter(this);
        listView.setAdapter(adapter);
        loadNewData();
    }

    /**
     * 政策解读接口
     */
    private void loadCoachData(final int i, final int e) {
        CoachModel.Body body = new CoachModel.Body();
        body.setSinceId(i);
        body.setMaxId(e);
        HttpUtil.doPost(Constants.Url.LIST_POLICY, body, new HttpResponse(this, CoachModel.class) {
            @Override
            public void onResult(Object obj) {
                CoachModel data = (CoachModel) obj;
                sinceId += data.getRows();
                if (adapter != null) {
                    if (i == 1)
                        adapter.setData(data.getData());
                    else
                        adapter.addData(data.getData());
                }
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                pullComplete();
            }
        });
    }

    private void loadNewData() {
        if (adapter != null) {
            sinceId = 1;
            int maxId = adapter.getCount() > 10 ? adapter.getCount() : 10;
            loadCoachData(sinceId, maxId);
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        loadNewData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        loadCoachData(sinceId, sinceId + 10);
    }

    private void pullComplete() {
        if (pullToRefreshListView != null)
            pullToRefreshListView.onRefreshComplete();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (adapter != null) {
            adapter.clear();
            adapter = null;
        }
    }
}
